package com.itheima.controller.backend.security;

import com.itheima.common.dto.CheckGroupDTO;
import com.itheima.common.dto.QueryPageBeanDTO;
import com.itheima.common.dto.SetmealDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private Configuration configuration;
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckItemService checkItemService;

//    /**
//     * 添加检查套餐
//     * @param setmeal
//     * @param checkgroupIds
//     * @return
//     */
//    @PostMapping("/add")
//    public Result addSetmeal(@RequestBody Setmeal setmeal,String checkgroupIds){
//        log.info("添加检查套餐");
//        setmealService.addSetmeal(setmeal,checkgroupIds);
//        return Result.success();
//    }

    /**
     * 添加检查套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result addSetmeal(@RequestBody Setmeal setmeal,String checkgroupIds){
        log.info("添加检查套餐");
        setmealService.addSetmeal(setmeal,checkgroupIds);
        Map params = getData();



        try {
            Template template = configuration.getTemplate("mobile_setmeal.ftl");
            template.process(params,new FileWriter("D:/mobile_setmeal.html"));

            List<Setmeal> setmealList = (List<Setmeal>) params.get("setmealList");
            for (Setmeal setmeals : setmealList) {
                Map detailParams = getDetailData(setmeals);
                Template detailTemplate = configuration.getTemplate("mobile_setmeal_detail.ftl");
                detailTemplate.process(detailParams,new FileWriter("D:/mobile_setmeal_detail_"+setmeals.getId()+".html"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success();
    }


    private Map getData(){
        Map<String,Object> data = new HashMap();
        List<Setmeal> setmealList = setmealService.getData();
        data.put("setmealList",setmealList);
        Map<String,Setmeal> map = new HashMap();
        for (Setmeal setmeal : setmealList) {
            map.put(setmeal.getName(),setmeal);
        }
        data.put("setmealMap",map);
        return data;
    }

    private Map getDetailData(Setmeal setmeal){
        Map<String,Object> data = new HashMap();
        List<CheckGroup> checkGroups = setmealService.getCheckGroupsBySetmealId(setmeal.getId());
        for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItems = checkItemService.getCheckitemsById(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        //封装详情数据
        data.put("setmeal",setmeal);
        return data;
    }

    /**
     * 删除检查套餐
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteSetmeal(@PathVariable Integer id){
        log.info("删除检查套餐");
        setmealService.deleteSetmeal(id);
        return Result.success();
    }

    /**
     * 修改检查套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PutMapping("/modify")
    public Result modifySetmeal(@RequestBody Setmeal setmeal,String checkgroupIds){
        log.info("修改检查套餐");
        setmealService.modifySetmeal(setmeal,checkgroupIds);
        return Result.success();
    }

    /**
     * 根据id查询检查套餐
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Result getSetmealById(Integer id){
        log.info("根据检查套餐id查询对应检查套餐");
        Setmeal setmeal = setmealService.getSetmealById(id);
        return Result.success(setmeal);
    }

    /**
     * 分页查询检查套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult pageSetmeal(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查套餐");
        PageResult pageResult = setmealService.pageSetmeal(queryPageBean);
        return pageResult;
    }

}
